package com.cx.service.impl;

import com.cx.mapper.PosttestMapper;
import com.cx.pojo.Posttest;
import com.cx.pojo.PosttestExample;
import com.cx.service.PostTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class PostTestServiceImpl implements PostTestService {

    @Autowired
    PosttestMapper posttestMapper;

    @Override
    public Integer insertPost(Posttest posttest) {
        Integer rows = posttestMapper.insertSelective(posttest);
        return rows;
    }

    // 查询所有的帖子
    @Override
    public List<Posttest> selectAllPostTest() {
        PosttestExample posttestExample = new PosttestExample();
        PosttestExample.Criteria criteria = posttestExample.createCriteria();

        List<Posttest> posttests = posttestMapper.selectByExample(posttestExample);
        return posttests;
    }

    // 根据id进行删除
    @Override
    public Integer deleteByPostTestId(Integer postId) {
        Integer rows = posttestMapper.deleteByPrimaryKey(postId);
        return rows;
    }

    // 根据id进行查找
    @Override
    public Posttest selectByPostTestId(Integer postId) {
        PosttestExample posttestExample = new PosttestExample();
        PosttestExample.Criteria criteria = posttestExample.createCriteria();
        criteria.andPostIdEqualTo(postId);
        List<Posttest> posttests = posttestMapper.selectByExample(posttestExample);
        return posttests.get(0);
    }

    // 根据帖名模糊查询
    @Override
    public List<Posttest> selectByPostTestName(String postName) {
        List<Posttest> posttests = posttestMapper.selectByPostTestName(postName);
        return posttests;
    }

    @Override
    public List<Posttest> selectByUserNameByPostTest(String userName) {
        PosttestExample posttestExample = new PosttestExample();
        PosttestExample.Criteria criteria = posttestExample.createCriteria();
        criteria.andUserNameEqualTo(userName);
        List<Posttest> posttests = posttestMapper.selectByExample(posttestExample);
        return posttests;
    }
}
